import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by lanouhn on 17/7/18.
 */


public class ImageRequest {
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //new一个URL对象
        URL url = new URL("https://www.baidu.com/link?url=K6o4ygujUCfHD1xfLVbfXmbOQZjcd_rUzBUYBZD4HMjrA0huSeb3D0uNF-f1wLqBtty5954TeViOJwvLGrTGE1LGxbBnzE1mHJRsxK06915JFw_etmevY9okcfNGiNrhqEBK4IMR_XIHfBE0WxhgSCBUG0prA4SWpaGJlfpzER3jm33mbAVvbiS1nds8Gq2Iw8Ot5NVdCm8RQmij1EW55p-j2F0mW7uJK6zEo4sD_hXDXHd4XwfDT7gTuKWHnxKqP5E9zg3EvwOtW8_srRKzEY-1ER4Gn13T0NapeYA_MlWuDHvWrKDN3uBgEQzDN4ia4y9OAL-z-ArzOJIvlFAo4dJdksxKs1X7rLSmScH9FKk5d3VCDTWUHP1CXCj4AIBOavn02YnYrdPQtaxlxIrkVP6VoTKBYQlnVTW-hP5d68o53gCK8BFJsfFLMxYAJErMKHNTdXub9cxK57_3ZUJ0v6-_yqs04prW-8YacjnL30I6JpDhS0TOYhzZ0wu38z9omXZMjV1TpEeRvXGvVTsngYoU_QGZ5mKFs9zIZt-ERMhLaTHWCgBSbkM17t-GZjAQsPZv-q9AHM8vP61Y62Q4euQjQxkOt2FCgd8ECazs9KskW0QD4lmDQsUkZ_DMsY_A&timg=https%3A%2F%2Fss0.bdstatic.com%2F94oJfD_bAAcT8t7mm9GUKT-xh_%2Ftimg%3Fimage%26quality%3D100%26size%3Db4000_4000%26sec%3D1500379263%26di%3D600edad095ecb91dc948235ac9c8a5fc%26src%3Dhttp%3A%2F%2Fimage.coolapk.com%2Fpicture%2F2016%2F1101%2F531994_1477997335_565.png.m.jpg&click_t=1500379347019&s_info=1334_939&wd=&eqid=838cedbf0005578100000004596df87f");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //设置请求方式为"GET"
        conn.setRequestMethod("GET");
        //超时响应时间为5秒
        conn.setConnectTimeout(5 * 1000);
        //通过输入流获取图片数据
        InputStream inStream = conn.getInputStream();
        //得到图片的二进制数据，以二进制封装得到数据，具有通用性
        byte[] data = readInputStream(inStream);
        //new一个文件对象用来保存图片，默认保存当前工程根目录
        File imageFile = new File("/Users/lanouhn/Desktop/1.png");
        //创建输出流
        FileOutputStream outStream = new FileOutputStream(imageFile);
        //写入数据
        outStream.write(data);
        //关闭输出流
        outStream.close();
    }
    public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len=inStream.read(buffer)) != -1 ){
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }
}


//import java.net.URL;
//import java.net.HttpURLConnection;
//import java.io.OutputStream;
//import java.io.InputStream;
//import java.io.FileOutputStream;
//import java.io.IOException;
//
//public class ImageRequest {
//    public void main (String[] args){
//
//        HttpURLConnection con=null;
//        //输入流
//        InputStream is=null;
//        //图片地址
//        string myurl = "http://www.baidu.com/baidu.php?url=gW_K00j_pD9o9Rl6mOccy_m7nfvumZIeqa8pE8HOmLgGZsPCB50zsZ_e8i8wGAd1mWwe1dlh22kQUrDtLgPwXWid3x9nGO-7Xf5DOFCwk6eResNn4UqVimp8GdKBw2P-EgtH1aaol62Rde59gTlqw6HhYaqStxlvsxq9epm8OkC0K7xJg6.DY_ifkR5Q9JuIWxDBjSc1PvGCfT7HJ2Xy7va26hUq1EZBmozNd4nheFklkECFmsu5ZI3qMWvXgUY42qhek_oozUEFhEGoLvunMZukzXyrOo6hC_iRcOQ9JuIWxDBjQ6Y33PEPiOi1JHukyAp7WGzXPxBC0.U1Yk0ZDqVSXS_0KY5UxMEojF3qrs0A-V5HnvPsKM5g93n0KdpHY0TA-b5HDY0APGujY1nWc0UgfqnW03rfKopHYs0ZFY5HbkPsK-pyfqnHD3rHNxnHcsnWFxnHD4PW00mhbqn0KVm1Ys0Z7spyfqn0Kkmv-b5H00ThIYmyTqn0KEIhsqnHcznW6VuZGxnHcYP1nVuZC0Uy-b5H00pg7Y5H00mycqn7ts0ANzu1Ys0ZKs5H00UMus5H08nj0snj0snj00Ugws5H00uAwETjYs0ZFJ5H00uMfqn0KspjYs0Aq15H00mMTqn0K8IjYs0ZPl5fKzug7Y5HDdnj01P16zPHm1Pjm0Tv-b5HIbujf3ryw-PhRsnjD3rjD0mynqnfKlIjYs0AdWgvuzUvYqn0KWThnqPjc3n10&ck=8016.1.9999.211.233.270.290.662";
//
//        //构造URL
//        URL url = new URL(myurl);
//
//        //打开连接
//        con = (HttpURLConnection)url.openConnection();
//        //判断网络响应
//        int code = con.getResponseCode();
//        if(code == 200){
//            is = con.getInputStream();
//
//            byte[] bs=new byte[1024];
//
//            int len;
//
//            OutputStream os = new FileOutputStream("/Users/lanouhn/Desktop/1.jpg");
//
//            while((len = is.read(bs))!=-1){
//                os.write(bs,0,len);
//            }
//            os.close();
//            is.close();
////        }
//
//
//    }
//}
